﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using Model;

namespace DAL
{
    class D_BasicInformation : IBasicInformation
    {
        WishDayEntities _e;
        public D_BasicInformation()
        {
            _e = new WishDayEntities();
        }
        public void Add(BasicInformation obj)
        {
            _e.BasicInformation.Add(obj);
            _e.SaveChanges();
        }

        public void Delete(int id)
        {
             var data=_e.BasicInformation.Find(id);
            _e.BasicInformation.Remove(data);
            _e.SaveChanges();
        }

        public BasicInformation FindById(int id)
        {
            return _e.BasicInformation.Find(id);
        }

        public BasicInform_and_BasicInformCus FindByTemplateID(int id)
        {
            var customs = _e.BasicInformationCustom.AsEnumerable();
            var basic = _e.BasicInformation.AsEnumerable();
            var list = from bb in basic
                       join bc in customs on bb.BasicID equals bc?.BasicID
                       where bb.TemplateID == id
                       select new Param_BasicInformCus
                       {
                           BICID = bc.BICID,
                           BICName = bc.BICName,
                           BICDescribe = bc.BICDescribe,
                           BasicID = bc.BasicID
                       };

            var data = from bb in basic
                       where bb.TemplateID == id
                       select new BasicInform_and_BasicInformCus
                       {
                           BasicID = bb.BasicID,
                           BasicName = bb.BasicName,
                           Birth = bb.Birth,
                           Tel = bb.Tel,
                           Email = bb.Email,
                           WorkingYears = bb.WorkingYears,
                           IMG = bb.IMG,
                           Sex = bb.Sex,
                           Marriage = bb.Marriage,
                           TemplateID = bb.TemplateID,
                           ListBasicInformCus = list == null ? null : list.Where(x => x.BasicID == bb.BasicID).ToList()
                       };
            return data.FirstOrDefault();
        }

        public List<BasicInformation> GetAll()
        {
            return _e.BasicInformation.ToList();
        }

        public void Update(int id, BasicInformation obj)
        {
            var data = _e.BasicInformation.Find(id);
            data.BasicName = obj.BasicName;
            data.Birth = obj.Birth;
            data.Tel = obj.Tel;
            data.Email = obj.Email;
            data.WorkingYears = obj.WorkingYears;
            data.IMG = obj.IMG;
            data.Sex = obj.Sex;
            data.Marriage = obj.Marriage;
            data.TemplateID = obj.TemplateID;
            _e.SaveChanges();
        }
    }
}
